﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Maps.MapControl;
using MapEntities;

namespace VirtualEarth.Business
{
    public class MapVisualizer
    {
        private Map map;
        private MapDataResult result;
        private GeoVisualization visualizationData;

        public MapVisualizer(Map map, MapDataResult result)
        {
            this.map = map;
            this.result = result;
        }

        public void Visualize()
        {
            map.Children.Clear();                 
            SetMapProperties();
            visualizationData = new GeoVisualization();
            CreateMapLayers();
        }

        private void SetMapProperties()
        {
            switch (result.MapProperties.BackgroundMode)
            {
                case Mode.Road:
                    {
                        map.Mode = new RoadMode();
                        break;
                    }
                case Mode.MercatorMode:
                    {
                        map.Mode = new Microsoft.Maps.MapControl.Core.MercatorMode();
                        break;
                    }
                case Mode.AerialWithLabels:
                    {
                        map.Mode = new AerialMode(true);
                        break;
                    }
                case Mode.Aerial:
                    {
                        map.Mode = new AerialMode(false);
                        break;
                    }
            }
            map.NavigationVisibility = result.MapProperties.ShowNavigationControls ? Visibility.Visible: Visibility.Collapsed;
            map.CopyrightVisibility = result.MapProperties.CopyrightVisible ? Visibility.Visible : Visibility.Collapsed;
            map.ScaleVisibility = result.MapProperties.ScaleVisible ? Visibility.Visible : Visibility.Collapsed;
            map.LogoVisibility = result.MapProperties.LogoVisible ? Visibility.Visible : Visibility.Collapsed;
            if (result.MapProperties.ZoomLevel > 0)
                map.ZoomLevel = result.MapProperties.ZoomLevel;
            map.CredentialsProvider = new ApplicationIdCredentialsProvider(result.MapProperties.BingMapKeY);
            if (result.MapProperties.Center.Lat > 0)
            {
                map.Center = new Location(result.MapProperties.Center.Lat, result.MapProperties.Center.Long);
            }
            map.MouseWheel += new MouseWheelEventHandler(map_MouseWheel);
            map.ViewChangeEnd += new EventHandler<MapEventArgs>(map_ViewChangeEnd);
            map.ViewChangeStart += new EventHandler<MapEventArgs>(map_ViewChangeStart);
            
        }

        void map_ViewChangeStart(object sender, MapEventArgs e)
        {
            
        }

        void map_ViewChangeEnd(object sender, MapEventArgs e)
        {
            foreach (GeoLayer xlayer in visualizationData.Layers)
            {
                if (((map.ZoomLevel <= xlayer.LayerData.EndZoomLevel) && map.ZoomLevel >= xlayer.LayerData.StartZoomLevel) || (xlayer.LayerData.StartZoomLevel == 0 && xlayer.LayerData.EndZoomLevel==0))
                    xlayer.Visibility = Visibility.Visible;
                else xlayer.Visibility = Visibility.Collapsed;
            }
        }

        void map_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            if (!result.MapProperties.MouseWhellZoom)
                e.Handled = true;
        }

        public GeoLayer LayerByName(string name)
        {
            foreach (var layer in visualizationData.Layers)
                if (layer.LayerData.Name == name)
                    return layer;
            return null;
        }

        private void SetItemLayer(GeoItem item)
        {
            GeoLayer layer = LayerByName(item.Item.CustomLayer);
            if (layer != null)
                item.Layer = layer;
        }

        private void CreateMapLayers()
        {
            foreach (Layer l in result.MapProperties.Layers)
            {
                GeoLayer layer = new GeoLayer(l);                
                visualizationData.Layers.Add(layer);
            }

            foreach (MapEntities.Shape shape in result.Shapes)
            {                                
                GeoShape s = new GeoShape(map, shape);                
                SetItemLayer(s);
                s.Visualize();
                visualizationData.Shapes.Add(s);

            }
            foreach (MapEntities.Symbol symbol in result.Symbols)
            {
                GeoSymbol s = new GeoSymbol(map, symbol);
                SetItemLayer(s);
                s.Visualize();
                visualizationData.Symbols.Add(s);
            }            
            CreateObjects();            
        }

        private void CreateObjects()
        {
            foreach (GeoLayer layer in visualizationData.Layers)
            {
                if(layer.LayerData.Visible)map.Children.Add(layer);
            }
        }
       
    }
}
